﻿using System;
using System.Windows.Forms;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace WindowsApplication2._8_加解密
{
    public partial class res_encode : Form
    {
        public res_encode()
        {
            InitializeComponent();
        }

        /// <summary>
        /// jia mi
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string strin = this.textBox1.Text;

            //byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(strin);
            //String enc = Convert.ToBase64String(encbuff);

            String enc = RESEncrypt(strin);

            this.textBox2.Text = enc;
        }

        public static readonly string VanclKey = "van12)(%";

        public static string RESEncrypt(string inputString)
        {
            byte[] rgbKey = null;
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            try
            {
                rgbKey = Encoding.UTF8.GetBytes(VanclKey.Substring(0, 8));
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                byte[] bytes = Encoding.UTF8.GetBytes(inputString);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                inputString = Convert.ToBase64String(stream.ToArray());
            }
            catch
            {
            }
            return inputString;
        }
        public static string RESDecrypt(string inputString)
        {
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            byte[] buffer3 = new byte[inputString.Length];
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(VanclKey.Substring(0, 8));
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                buffer3 = Convert.FromBase64String(inputString);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write);
                stream2.Write(buffer3, 0, buffer3.Length);
                stream2.FlushFinalBlock();
                inputString = new UTF8Encoding().GetString(stream.ToArray());
            }
            catch
            {
            }
            return inputString;
        }

 

 


 



        /// <summary>
        /// jiemi
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string strin = this.textBox1.Text;

            //byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(strin);
            //String enc = Convert.FromBase64String(strin);

            //byte[] encbuff = Convert.FromBase64String(strin);
            //String enc = System.Text.Encoding.UTF8.GetString(encbuff);

            String enc = RESDecrypt(strin);

            this.textBox2.Text = enc;
        }

        private void res_encode_Load(object sender, EventArgs e)
        {

        }
    }
}